//
//  BKSegmentView.m
//  lg_zhinengbeikao_english
//
//  Created by 盛超 on 2019/1/7.
//  Copyright © 2019年 zhukai. All rights reserved.
//

#import "BKSegmentView.h"

@interface BKSegmentView ()

@property (nonatomic,strong) UIImageView *bgView;
@property (nonatomic,strong) UILabel *labelLeft;
@property (nonatomic,strong) UILabel *labelRight;
@property (nonatomic,assign) CGSize segmentSize;

@end

@implementation BKSegmentView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame SegmentSize:(CGSize)segmentSize {
    if (self = [super initWithFrame:frame]) {
        _segmentSize = segmentSize;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _bgView = [[UIImageView alloc] init];
    _bgView.backgroundColor = [UIColor blueColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    UIView *bgView2 = [[UIView alloc] init];
    bgView2.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView2];
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(self.segmentSize.width*2, self.segmentSize.height));
    }];
    bgView2.layer.cornerRadius = self.segmentSize.height/4;
    bgView2.layer.borderWidth = 1;
    bgView2.layer.borderColor = [UIColor whiteColor].CGColor;
    bgView2.layer.masksToBounds = YES;

    _labelLeft = [[UILabel alloc] init];
    _labelLeft.text = @"左边";
    _labelLeft.backgroundColor = [UIColor redColor];
    _labelLeft.textAlignment = NSTextAlignmentCenter;
    _labelLeft.textColor = [UIColor whiteColor];
    [self addSubview:_labelLeft];
    [_labelLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(self.segmentSize);
    }];
    _labelLeft.layer.cornerRadius = self.segmentSize.height/4;
    _labelLeft.layer.borderWidth = 1;
    _labelLeft.layer.borderColor = [UIColor whiteColor].CGColor;
    _labelLeft.layer.masksToBounds = YES;
    
    _labelRight = [[UILabel alloc] init];
    _labelRight.text = @"右边";
    _labelRight.backgroundColor = [UIColor whiteColor];
    _labelRight.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_labelRight];
    [_labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(self.segmentSize);
    }];
    _labelRight.layer.cornerRadius = self.segmentSize.height/4;
    _labelRight.layer.borderWidth = 1;
    _labelRight.layer.borderColor = [UIColor whiteColor].CGColor;
    _labelRight.layer.masksToBounds = YES;
    
    if (isPhoneXAll) {
        [bgView2 mas_updateConstraints:^(MASConstraintMaker *make) {
             make.centerY.equalTo(self).offset((64-self.segmentSize.height)/2);
        }];
        
        [_labelLeft mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset((64-self.segmentSize.height)/2);
        }];
        [_labelRight mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset((64-self.segmentSize.height)/2);
        }];
    }
    
    //调用layoutIfNeeded后就可以在Masonry布局后获取Frame
    [self layoutIfNeeded];
    
    //半边圆角
   // [self addToView:bgView2 RounedCorner:UIRectCornerTopLeft|UIRectCornerBottomLeft|UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(self.segmentSize.height/4, self.segmentSize.height/4)];
   // [self addToView:_labelLeft RounedCorner:UIRectCornerTopLeft|UIRectCornerBottomLeft|UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(self.segmentSize.height/4, self.segmentSize.height/4)];
  // [self addToView:_labelRight RounedCorner:UIRectCornerTopLeft|UIRectCornerBottomLeft|UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(self.segmentSize.height/4, self.segmentSize.height/4)];
    
    
}

//设置哪几个边是圆角
- (void)addToView:(UIView*)roundView RounedCorner:(UIRectCorner)corner withRadii:(CGSize)radii {
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundView.bounds byRoundingCorners:corner cornerRadii:radii];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = roundView.bounds;
    shapeLayer.path = maskPath.CGPath;
    roundView.layer.mask = shapeLayer;
}

@end
